/*
 * Copyright (C) 2015-2017 Alibaba Group Holding Limited
 */

#ifndef OTA_HAL_DIGEST_H
#define OTA_HAL_DIGEST_H

#define OTA_SHA256_HASH_SIZE         (32)
#define OTA_MD5_HASH_SIZE            (16)

#define OTA_MD5_TOTAL_SIZE           (2)
#define OTA_MD5_STATE_SIZE           (4)
#define OTA_MD5_BUF_SIZE             (64)

#define OTA_SHA256_TOTAL_SIZE        (2)
#define OTA_SHA256_STATE_SIZE        (8)
#define OTA_SHA256_BUF_SIZE          (64)

/*Verify API*/
#if defined OTA_CONFIG_ITLS
typedef struct {
    size_t size;
    void *ali_ctx;
} ota_md5_context, ota_sha256_context;
#else
typedef struct
{
    unsigned int  total[OTA_MD5_TOTAL_SIZE];
    unsigned int  state[OTA_MD5_STATE_SIZE];
    unsigned char buffer[OTA_MD5_BUF_SIZE];
} ota_md5_context;

typedef struct {
    unsigned int  total[OTA_SHA256_TOTAL_SIZE];
    unsigned int  state[OTA_SHA256_STATE_SIZE];
    unsigned char buffer[OTA_SHA256_BUF_SIZE];
    int is224;
} ota_sha256_context;
#endif

/*Verify API*/
/*SHA256*/
void ota_sha256_free(ota_sha256_context *ctx);
void ota_sha256_init(ota_sha256_context *ctx);
void ota_sha256_starts(ota_sha256_context *ctx, int is224);
void ota_sha256_update(ota_sha256_context *ctx, const unsigned char *input, unsigned int ilen);
void ota_sha256_finish(ota_sha256_context *ctx, unsigned char output[32]);
/*MD5*/
void ota_md5_free(ota_md5_context *ctx);
void ota_md5_init(ota_md5_context *ctx);
void ota_md5_starts(ota_md5_context *ctx);
void ota_md5_update(ota_md5_context *ctx, const unsigned char *input, unsigned int ilen);
void ota_md5_finish(ota_md5_context *ctx, unsigned char output[16]);
/*RSA*/
int ota_rsa_pubkey_verify(const unsigned char *pubkey_n,
                          const unsigned char *pubkey_e,
                          unsigned int pubkey_n_size,
                          unsigned int pubkey_e_size,
                          const unsigned char *dig,
                          unsigned int dig_size,
                          const unsigned char *sig,
                          unsigned int sig_size);
#endif /*OTA_HAL_DIGEST_H*/

